package cn.iocoder.yudao.module.design.controller.admin.useraccount;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.useraccount.vo.*;
import cn.iocoder.yudao.module.design.convert.useraccount.UserAccountConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.useraccount.UserAccountDO;
import cn.iocoder.yudao.module.design.service.useraccount.UserAccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 用户账户信息")
@RestController
@RequestMapping("/design/user-account")
@Validated
public class UserAccountController {

    @Resource
    private UserAccountService userAccountService;

    @PostMapping("/create")
    @Operation(summary ="创建用户账户信息")
    @PreAuthorize("@ss.hasPermission('design:user-account:create')")
    public CommonResult<Long> createUserAccount(@Valid @RequestBody UserAccountCreateReqVO createReqVO) {
        return success(userAccountService.createUserAccount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新用户账户信息")
    @PreAuthorize("@ss.hasPermission('design:user-account:update')")
    public CommonResult<Boolean> updateUserAccount(@Valid @RequestBody UserAccountUpdateReqVO updateReqVO) {
        userAccountService.updateUserAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除用户账户信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:user-account:delete')")
    public CommonResult<Boolean> deleteUserAccount(@RequestParam("id") Long id) {
        userAccountService.deleteUserAccount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得用户账户信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:user-account:query')")
    public CommonResult<UserAccountRespVO> getUserAccount(@RequestParam("id") Long id) {
        UserAccountDO userAccount = userAccountService.getUserAccount(id);
        return success(UserAccountConvert.INSTANCE.convert(userAccount));
    }

    @GetMapping("/list")
    @Operation(summary ="获得用户账户信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:user-account:query')")
    public CommonResult<List<UserAccountRespVO>> getUserAccountList(@RequestParam("ids") Collection<Long> ids) {
        List<UserAccountDO> list = userAccountService.getUserAccountList(ids);
        return success(UserAccountConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得用户账户信息分页")
    @PreAuthorize("@ss.hasPermission('design:user-account:query')")
    public CommonResult<PageResult<UserAccountRespVO>> getUserAccountPage(@Valid UserAccountPageReqVO pageVO) {
        PageResult<UserAccountDO> pageResult = userAccountService.getUserAccountPage(pageVO);
        return success(UserAccountConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出用户账户信息 Excel")
    @PreAuthorize("@ss.hasPermission('design:user-account:export')")
   
    public void exportUserAccountExcel(@Valid UserAccountExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserAccountDO> list = userAccountService.getUserAccountList(exportReqVO);
        // 导出 Excel
        List<UserAccountExcelVO> datas = UserAccountConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户账户信息.xls", "数据", UserAccountExcelVO.class, datas);
    }

}
